package com.ursu.server.handler;

import java.util.logging.Logger;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;
import com.ursu.shared.actions.Logout;
import com.ursu.shared.actions.LogoutResult;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.gwtplatform.dispatch.server.ExecutionContext;
import com.gwtplatform.dispatch.shared.ActionException;

public class LogoutActionHandler implements ActionHandler<Logout, LogoutResult> {
	
	private final Provider<HttpServletRequest> requestProvider;
	private static final Logger log = Logger.getLogger("LogoutActionHandler");

	@Inject
	public LogoutActionHandler(final ServletContext servletContext,
		      final Provider<HttpServletRequest> requestProvider) {
		this.requestProvider = requestProvider;
	}

	@Override
	public LogoutResult execute(Logout action, ExecutionContext context)
			throws ActionException {
		
		HttpSession session = requestProvider.get().getSession();
		session.removeAttribute("login.authenticated");
		session.invalidate();
		
		log.severe("The user's session was invalidated");
		return null;
	}

	@Override
	public void undo(Logout action, LogoutResult result, ExecutionContext context)
			throws ActionException {
	}

	@Override
	public Class<Logout> getActionType() {
		return Logout.class;
	}
}
